﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace GoobizFrame.Windows.Controls
{

    /// <summary>
    /// XtraHNavigator:
    /// <br>- Utility: Thay thế embeded navigator trên gridcontrol</br>
    /// <br>- Owner: Phuongphan</br>
    /// </summary>
    public partial class XtraHNavigator : DevExpress.XtraEditors.XtraUserControl
    {
        #region enum
        /// <summary>
        /// Hướng xếp button
        /// </summary>
        public enum NavigatorOrientation
        {
            Vertical,
            Horizontal
        }

        /// <summary>
        /// Cách thức gom nhóm các button theo chức năng
        /// </summary>
        public enum NavigatorButtonModes
        {
            ShowEditButtons,
            ShowNavigators,
            ShowBoth,
            None
        }
        #endregion
        int _ButtonsCount = 11;
        public XtraHNavigator()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Thay đổi trạng thái Enabled các button dựa theo trang thái của GridControl.EmbeddedNavigator.NavigatableControl
        /// </summary>
        void SetActionEnabled()
        {
            try
            {
                controlNavigator.Enabled = gridControl.Enabled;

                controlNavigator.Buttons.Append.Enabled = gridControl.EmbeddedNavigator.Buttons.Append.Enabled;
                controlNavigator.Buttons.Edit.Enabled = gridControl.EmbeddedNavigator.Buttons.Edit.Enabled;
                controlNavigator.Buttons.CancelEdit.Enabled = gridControl.EmbeddedNavigator.Buttons.CancelEdit.Enabled;
                controlNavigator.Buttons.EndEdit.Enabled = gridControl.EmbeddedNavigator.Buttons.EndEdit.Enabled;
                controlNavigator.Buttons.First.Enabled = gridControl.EmbeddedNavigator.Buttons.First.Enabled;
                controlNavigator.Buttons.Last.Enabled = gridControl.EmbeddedNavigator.Buttons.Last.Enabled;
                controlNavigator.Buttons.Next.Enabled = gridControl.EmbeddedNavigator.Buttons.Next.Enabled;
                controlNavigator.Buttons.NextPage.Enabled = gridControl.EmbeddedNavigator.Buttons.NextPage.Enabled;
                controlNavigator.Buttons.PrevPage.Enabled = gridControl.EmbeddedNavigator.Buttons.PrevPage.Enabled;
                controlNavigator.Buttons.Prev.Enabled = gridControl.EmbeddedNavigator.Buttons.Prev.Enabled;
                controlNavigator.Buttons.Remove.Enabled = gridControl.EmbeddedNavigator.Buttons.Remove.Enabled;

            }
            catch (Exception ex) {
                    GoobizFrame.Windows.Forms.MessageDialog.Show(ex.Message, ex.ToString(), this.GetType().Name);
            }
        }

        /// <summary>
        /// Thay đổi trạng thái Visible của các button
        /// </summary>
        /// <param name="visible"></param>
        void SetEmbeddedNavigatorVisible(bool visible)
        {
            gridControl.EmbeddedNavigator.Buttons.Append.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.CancelEdit.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.Edit.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.EndEdit.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.First.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.Last.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.Next.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.NextPage.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.Prev.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.PrevPage.Visible = visible;
            gridControl.EmbeddedNavigator.Buttons.Remove.Visible = visible;
        }

        #region Properties
        private NavigatorOrientation orientation = NavigatorOrientation.Horizontal;
        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(XtraHNavigator.NavigatorOrientation.Horizontal)]
        public NavigatorOrientation Orientation
        {
            get { return orientation; }
            set
            {
                orientation = value;
                //SetSize();
            }
        }

        private NavigatorButtonModes buttonmodes = NavigatorButtonModes.ShowBoth;
        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(XtraHNavigator.NavigatorButtonModes.ShowBoth)]
        public NavigatorButtonModes ButtonModes
        {
            get { return buttonmodes; }
            set
            {
                buttonmodes = value;
                switch (buttonmodes)
                {
                    case NavigatorButtonModes.None:
                        controlNavigator.Buttons.Append.Visible = false;
                        controlNavigator.Buttons.Edit.Visible = false;
                        controlNavigator.Buttons.Remove.Visible = false;
                        controlNavigator.Buttons.CancelEdit.Visible = false;
                        controlNavigator.Buttons.EndEdit.Visible = false;

                        controlNavigator.Buttons.First.Visible = false;
                        controlNavigator.Buttons.PrevPage.Visible = false;
                        controlNavigator.Buttons.Prev.Visible = false;
                        controlNavigator.Buttons.Next.Visible = false;
                        controlNavigator.Buttons.NextPage.Visible = false;
                        controlNavigator.Buttons.Last.Visible = false;

                        VisibleTextLocation = false;

                        this.Visible = false;
                        if (gridControl != null)
                        {
                            gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.Center;
                            gridControl.Refresh();
                        }
                        _ButtonsCount = 0;
                        break;

                    case NavigatorButtonModes.ShowBoth:
                        controlNavigator.Buttons.Append.Visible = true;
                        controlNavigator.Buttons.Edit.Visible = true;
                        controlNavigator.Buttons.Remove.Visible = true;
                        controlNavigator.Buttons.CancelEdit.Visible = true;
                        controlNavigator.Buttons.EndEdit.Visible = true;

                        controlNavigator.Buttons.First.Visible = true;
                        controlNavigator.Buttons.PrevPage.Visible = true;
                        controlNavigator.Buttons.Prev.Visible = true;
                        controlNavigator.Buttons.Next.Visible = true;
                        controlNavigator.Buttons.NextPage.Visible = true;
                        controlNavigator.Buttons.Last.Visible = true;

                        VisibleTextLocation = true;
                        this.Visible = true;
                        if (gridControl != null)
                        {
                            gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.None;
                            gridControl.Refresh();
                        }
                        _ButtonsCount = 11;
                        break;

                    case NavigatorButtonModes.ShowEditButtons:
                        controlNavigator.Buttons.Append.Visible = true;
                        controlNavigator.Buttons.Edit.Visible = true;
                        controlNavigator.Buttons.Remove.Visible = true;
                        controlNavigator.Buttons.CancelEdit.Visible = true;
                        controlNavigator.Buttons.EndEdit.Visible = true;

                        controlNavigator.Buttons.First.Visible = false;
                        controlNavigator.Buttons.PrevPage.Visible = false;
                        controlNavigator.Buttons.Prev.Visible = false;
                        controlNavigator.Buttons.Next.Visible = false;
                        controlNavigator.Buttons.NextPage.Visible = false;
                        controlNavigator.Buttons.Last.Visible = false;

                        VisibleTextLocation = false;
                        this.Visible = true;
                        _ButtonsCount = 5;
                        break;

                    case NavigatorButtonModes.ShowNavigators:
                        controlNavigator.Buttons.Append.Visible = false;
                        controlNavigator.Buttons.Edit.Visible = false;
                        controlNavigator.Buttons.Remove.Visible = false;
                        controlNavigator.Buttons.CancelEdit.Visible = false;
                        controlNavigator.Buttons.EndEdit.Visible = false;

                        controlNavigator.Buttons.First.Visible = true;
                        controlNavigator.Buttons.PrevPage.Visible = true;
                        controlNavigator.Buttons.Prev.Visible = true;
                        controlNavigator.Buttons.Next.Visible = true;
                        controlNavigator.Buttons.NextPage.Visible = true;
                        controlNavigator.Buttons.Last.Visible = true;

                        VisibleTextLocation = true;
                        this.Visible = true;
                        if (gridControl != null)
                        {
                            gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.None;
                            gridControl.Refresh();
                        }
                        _ButtonsCount = 6;
                        break;
                }

                SetSizeofNavigator();
            }
        }
        
        
        private DevExpress.XtraGrid.GridControl gridControl;     
        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(null)]
        public DevExpress.XtraGrid.GridControl GridControl
        {
            get { return gridControl; }
            set
            {
                gridControl = value;
               
                if (value != null)
                {
                    gridControl.UseEmbeddedNavigator = true;
                    gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.None;

                    controlNavigator.TextStringFormat = gridControl.EmbeddedNavigator.TextStringFormat;
                    controlNavigator.NavigatableControl = gridControl;

                    SetEmbeddedNavigatorVisible(false);
                    gridControl.Paint += new PaintEventHandler(gridControl_Paint);
                    SetActionEnabled();
                }
            }
        }

        void gridControl_Paint(object sender, PaintEventArgs e)
        {
            this.SetActionEnabled();
        }


        [Category("XtraHNavigator")]
        [Description("...")]
        public string TextStringFormat
        {
            get { return controlNavigator.TextStringFormat; }
            set
            { 
                controlNavigator.TextStringFormat = value;
                controlNavigator.Update();

                if (gridControl != null)
                {
                    gridControl.EmbeddedNavigator.TextStringFormat = value;
                    gridControl.Refresh();
                }
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleTextLocation
        {
            get { return (controlNavigator.TextLocation != NavigatorButtonsTextLocation.None); }
            set 
            {
                if (value)
                {
                    controlNavigator.TextLocation = NavigatorButtonsTextLocation.Center;
                    if (gridControl != null)
                    {
                        gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.None;
                        gridControl.Refresh();
                    }
                }
                else
                {
                    controlNavigator.TextLocation = NavigatorButtonsTextLocation.None;
                    if (gridControl != null)
                    {
                        gridControl.EmbeddedNavigator.TextLocation = NavigatorButtonsTextLocation.Center;
                        gridControl.Refresh();
                    }
                }
                SetSizeofNavigator();
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisibleNext
        {
            get { return controlNavigator.Buttons.Next.Visible; }
            set 
            {
                controlNavigator.Buttons.Next.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
            }
        }
  
        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleEndEdit
        {
            get { return controlNavigator.Buttons.EndEdit.Visible; }
            set {
                controlNavigator.Buttons.EndEdit.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.EndEdit.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisiblePrev
        {
            get { return controlNavigator.Buttons.Prev.Visible; }
            set {
                controlNavigator.Buttons.Prev.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.Prev.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleRemove
        {
            get { return controlNavigator.Buttons.Remove.Visible; }
            set {
                controlNavigator.Buttons.Remove.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.Remove.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisibleLast
        {
            get { return controlNavigator.Buttons.Last.Visible; }
            set {
                controlNavigator.Buttons.Last.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.Last.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleAppend
        {
            get { return controlNavigator.Buttons.Append.Visible; }
            set {
                controlNavigator.Buttons.Append.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.Append.Visible = !value;
            }
        }
 
        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleEdit
        {
            get { return controlNavigator.Buttons.Edit.Visible; }
            set {
                controlNavigator.Buttons.Edit.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.Append.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(true)]
        public bool VisibleCancelEdit
        {
            get { return controlNavigator.Buttons.CancelEdit.Visible; }
            set {
                controlNavigator.Buttons.CancelEdit.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.CancelEdit.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisiblePageNext
        {
            get { return controlNavigator.Buttons.NextPage.Visible; }
            set {
                controlNavigator.Buttons.NextPage.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.NextPage.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisiblePagePrev
        {
            get { return controlNavigator.Buttons.PrevPage.Visible; }
            set {
                controlNavigator.Buttons.PrevPage.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.PrevPage.Visible = !value;
            }
        }

        [Category("XtraHNavigator")]
        [Description("...")]
        [DefaultValue(false)]
        public bool VisibleFirst
        {
            get { return controlNavigator.Buttons.First.Visible; }
            set {
                controlNavigator.Buttons.First.Visible = value;
                _ButtonsCount = (value) ? _ButtonsCount++ : _ButtonsCount--;
                SetSizeofNavigator();
                //if (gridControl != null) gridControl.EmbeddedNavigator.Buttons.First.Visible = !value;
            }
        }

        
        #endregion

        #region event ButtonClick

        /// <summary>
        /// - event: ButtonClick
        /// - handler: NavigatorButtonClickEventHandler
        /// </summary>
        public event NavigatorButtonClickEventHandler ButtonClick;
   
        private void controlNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (ButtonClick != null)
                ButtonClick(sender, e);
        }
        #endregion

        void SetSizeofNavigator()
        {
            int _sizeof_TextStringFormat = 
                (controlNavigator.TextLocation!= NavigatorButtonsTextLocation.None)
                ? MdiUtils.SizeHelper.GetTextSizeInPixel(
                    controlNavigator.TextStringFormat
                    , this.CreateGraphics()
                    , controlNavigator.Font).Width
                : 0;

            controlNavigator.Size = new Size(
               this.Height * _ButtonsCount + _sizeof_TextStringFormat
                , this.Height);

            //test
            //labelControl1.Text = string.Format("B.Count:{3} - W{0} x H{1} - {2}"
            //    , new object[] { controlNavigator.Width, controlNavigator.Height, _sizeof_TextStringFormat, _ButtonsCount });

            controlNavigator.Update();
        }

        private void XtraHNavigator_SizeChanged(object sender, EventArgs e)
        {
            SetSizeofNavigator();
        }


     
    }
}
